package fivm.common.client.sys;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fi.common.log.FiLog;

import fivm.cross.platform.ficommon.distinguish.FiObjectId;
import fivm.cross.platform.steamobj.app.AppConfigs;
import fivm.cross.platform.steamobj.sys.DeviceConfigs;
import fivm.cross.platform.steamobj.sys.DeviceControlStream;

public class DeviceControlClient {
	private final static String TAG=DeviceControlClient.class.getSimpleName();
	private final String GetAppConfigsByIdUrl="/app/getAppConfigsById";
	private final String AppConfigsClientPath="/data/local/tmp/vmtools/sys/appsconfig";
	
	private DeviceControlStream deviceControlStream;

	private HashMap<String, AppConfigs> mapAppConfig=new HashMap<String, AppConfigs>();
	
	
	public DeviceControlClient(DeviceControlStream deviceControlStream){
		this.deviceControlStream=deviceControlStream;
	}

	/**
	 * */
	private HashMap<String ,AppConfigs> httpRequestMapAppConfigsByListId(List<FiObjectId> listObjectId) throws UnsupportedEncodingException {
		HashMap<String ,AppConfigs> retMap=new HashMap<String ,AppConfigs>();
		for(FiObjectId objectId: listObjectId){
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			AppConfigs appConfigs=httpRequestAppConfigsById(objectId);
			if(appConfigs==null){
				FiLog.appLogW(TAG,"get the appconfigs is null :  objectId is : "+objectId);
				continue;
			}
			retMap.put(objectId.toString(), appConfigs);
		}
		return retMap;
	}
	
	
	//通过id找到appConfig
    public AppConfigs getAppConfigsById(FiObjectId objectId){
    	return getAppConfigsById(objectId.toString());
    }
    
    public AppConfigs getAppConfigsById(String id){
    	return mapAppConfig.get(id);
    }

	//通过appName找到appConfig
	public AppConfigs findAppConfigs(String appName){
		Iterator<HashMap.Entry<String ,AppConfigs>> entries=mapAppConfig.entrySet().iterator();
		while(entries.hasNext()){
			Map.Entry<String, AppConfigs> entry = entries.next();
			if(entry.getValue().getName().equals(appName)){
				return entry.getValue();
			}
		}
		return null;
	}
    public void updateMapAppConfigsFromServer() throws UnsupportedEncodingException {
    	mapAppConfig=httpRequestMapAppConfigsByListId(deviceControlStream.getListAppConfigsId());
    }
    
    public void updateMapAppConfigsFromFile(){
    	mapAppConfig=getAppConfigListFromFile(deviceControlStream.getListAppConfigsId());
    }
    

    public void appConfigListToFile(){
		Iterator<HashMap.Entry<String ,AppConfigs>> entries=mapAppConfig.entrySet().iterator();
		while(entries.hasNext()){
			Map.Entry<String, AppConfigs> entry = entries.next();
			String fileName=AppConfigsClientPath+"/"+entry.getKey();
			AppConfigs appConfigs=entry.getValue();
			appConfigs.toFile(fileName);
		}
    }
    
    public HashMap<String ,AppConfigs> getAppConfigListFromFile(List<FiObjectId> listObjectId){
    	for(FiObjectId objectid: listObjectId){
    		String fileName=AppConfigsClientPath+"/"+objectid.toString();
    		AppConfigs appConfigs=AppConfigs.fromFile(fileName, AppConfigs.class);
    		mapAppConfig.put(objectid.toString(), appConfigs);
    	}
    	return mapAppConfig;
    }
    
    
    /**
     * 通过object id 向服务端请求取得app config
     * */
	private AppConfigs httpRequestAppConfigsById(FiObjectId appConfigsId) throws UnsupportedEncodingException {
		return httpRequestAppConfigsById(appConfigsId.toString());
	}
	private AppConfigs httpRequestAppConfigsById(String appConfigsId) throws UnsupportedEncodingException {
		DeviceConfigs deviceConfigs=deviceControlStream.getDeviceConfigs();
		String url=deviceConfigs.genUrl(GetAppConfigsByIdUrl);
		HashMap<String,String> param=new HashMap<String,String>();
		param.put("AppConfigsId", appConfigsId);
		return AppConfigs.toHttp(url, param, AppConfigs.class);
	}
    
  
	
	
	
	
	
	
	public DeviceControlStream getDeviceControlStream() {
		return deviceControlStream;
	}

	public void setDeviceControlStream(DeviceControlStream deviceControlStream) {
		this.deviceControlStream = deviceControlStream;
	}
	
}
